<template>
  <y-dialog @open="open" @close="emit('close')" width="1000px" append-to-body>
    <el-form
      ref="addRef"
      :model="form"
      :rules="rules"
      size="default"
      label-width="120px"
    >
      <el-row>
        <el-col>
          <el-form-item label="名称/编号" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入名称/编号"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属产品" prop="product">
            <y-select
              v-model="form.product"
              placeholder="请选择所属产品"
              :optionsFunc="listProduct"
              @change="changeProductOptions"
              :disabled="props.operate === 'update'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="父版本" prop="parent">
            <y-tree-select
              v-model="form.parent"
              placeholder="请选择父版本"
              :optionsFunc="treeselect"
              :optionsParams="queryParams"
              @change="handleParentChanged"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="源码地址" prop="source">
            <el-input
              v-model="form.source"
              placeholder="请输入源代码库地址，如SVN、GIT仓库地址"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="下载地址" prop="download">
            <el-input
              v-model="form.download"
              placeholder="请输入版本下载地址"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="负责人" prop="owner">
            <y-select
              v-model="form.owner"
              placeholder="请选择所负责人"
              :optionsFunc="listUser"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <y-radio v-model="form.status" :options="sys_enable_disable" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入备注信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="标签" prop="tags">
            <y-tag v-model="form.tags" :closable="true" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </y-dialog>
</template>

<script setup name="AuVer">
import { listProduct } from "@/api/pms/product";
import { treeselect } from "@/api/pms/version";
import { listUser } from "@/api/sys/user";

const { proxy } = getCurrentInstance();
const { sys_enable_disable } = proxy.useDict("sys_enable_disable");

const emit = defineEmits(["submited", "close"]);

const data = reactive({
  form: {},
  queryParams: {
    product: undefined,
  },
  rules: {
    product: [{ required: true, message: "产品名称不能为空", trigger: "blur" }],
    name: [{ required: true, message: "版本名称不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

const props = defineProps({
  id: { type: String, default: "" },
  operate: { type: String, default: "add" },
  getMethod: { type: Function, default: undefined },
  addMethod: { type: Function, default: undefined },
  updateMethod: { type: Function, default: undefined },
});

/** 获取选中的产品对象信息 */
function changeProductOptions(val) {
  queryParams.value.product = val;
  form.value.parent = undefined;
}

/** 限制父版本非当前版本 */
function handleParentChanged(val) {
  if (form.value.id == val) {
    proxy.$modal.msgWarning("不应选择该版本为父版本");
    form.value.parent = undefined;
  }
}

/** 更新标签方法 */
function handleTagsUpdate(data) {
  form.value.tags = data;
}

/** 打开对话框事件 */
function open() {
  switch (props.operate) {
    case "update":
      props.getMethod(props.id).then((res) => {
        form.value = res.data;
        queryParams.value.product = form.value.product.value;
      });
      break;
    default:
      reset();
      break;
  }
}

/** 重置Form方法 */
function reset() {
  form.value = {
    status: "0",
  };
  proxy.resetForm("addRef");
}

/** 提交按钮事件 */
function submit() {
  proxy.$refs["addRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        props.updateMethod(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功");
          submited();
        });
      } else {
        props.addMethod(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功");
          submited();
        });
      }
    }
  });
}

/** submited事件 */
function submited() {
  emit("submited");
  emit("close");
}
</script>
